
$(function(){
    $('.js-nav li').click( function(){
        var title = $(this).find('a').text();
        $("#item-content .js-title").html(title);
        var string = "name=" + title;
        $.ajax({
            type: "get",
            url: "DealCmd.html",
            data: string,
            complete: setdatafromres
        });
        return;
    });
    
    $("#item-content").find('.js-close').click(function() {
         clearContentDisplay($("#item-content"));
    });

    $("#download li").click(function(){
      var i = $(this).index();
      var title = $('#download').find('a').eq(i-1).text();
      $.ajax({
        url: "/index/DownloadFiles.html",
        data: "title=" + title,
        success: function(){
          //alert("success");
        }
      });
    });

    $("#g-uploadfile button").on("click", function(e){

        var fd = new FormData($("form[name=uploadfile]")[0]);

        $.ajax({
            url: "/index/Uploadfile.html",
            data: fd,
            type: "POST",
            processData: false,
            contentType: false,
            complete: resResult
        });
    });

    var indx = 0;
    var filenames = "";
    $("#g-uploadfile-test .js-add").on("click", function(e){
        var filename = $(this).siblings('input').val();
        if (indx == 0)
        filenames+=filename;
        else
        filenames+=","+filename;
        indx++;
        $(this).siblings('input').attr("disabled", "disabled");
        $(this).before("<br/><input type='file' name='filename"+indx+"'/>");
    });

    $("#g-uploadfile-test .js-submit").on("click", function(e){
        alert(filenames);
        filenames = "";
        indx = 0;
    });
});

function resResult(data, textStatus) {
    if (textStatus == "success") {
        alert("上传成功，告诉管理员吧");
    } else {
        alert("上传失败");
    }
}

var overlay = "<div id='jk_overlay'></div>";

function setdatafromres(data, textStatus) {

    $content = $("#item-content");
    // $each.Name, $each.Title, $each.Content, $each.Author
    $content.find('section').html(data.responseText);

    $content.removeClass('no-display');
    $content.addClass('item-common');
    $content.addClass('jkcenter_absolute');

    $content.wrap(overlay);
    
     $("#item-content .js-content > h3").click(function(){
      var item = $(this).next();
      if (item.css("display") == "none")
          item.show('slow');
      else 
          item.hide('slow');
    });
}

function clearContentDisplay(id) {
    id.find('header .js-title').html("");
    id.find('section').html("");
    id.addClass('no-display');
    id.removeClass('jkcenter_absolute');
    id.unwrap();
}
